# -*- coding:utf-8 -*-



# 搜索斗图
def cx_doutu(text, botID, to_wxID):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
    }
    # 获取图片内容
    html = requests.get(url='http://api.wpbom.com/api/bucket.php?msg=' + text, headers=headers)
    url_path = html.content.decode('utf-8')
    arr = url_path.split('/')
    img_downPath = basedir + '\\user_img\\' + arr[-1]
    IMG = requests.get(url=url_path, headers=headers)
    with open(img_downPath, 'wb') as fp:
        for data in IMG.iter_content(1024):
            fp.write(data)
    time.sleep(random.randint(1, 5))
    send_image_msg(botID, to_wxID, img_downPath)


# 抖音视频无水印解析和背景音乐解析
def douyin_videoJX(v_url, mode, botID, to_wxID):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
    }
    html = requests.get(url='https://api.oick.cn/douyin/api.php?url=' + v_url, headers=headers)
    jsonarr = json.loads(html.content.decode('utf-8'))
    if jsonarr['play'] == False:
        send_text_msg(botID, to_wxID, "ERROR：Please enter a correct URL!")
        return
    # 时间戳
    sjc = str(int(time.time()))
    if mode == 'music':
        Music_downPath = basedir + '\\user_img\\' + sjc + '.mp3'
        getMusicdata = requests.get(url=jsonarr['music'], stream=True, headers=headers)
        with open(Music_downPath, 'wb') as fp:
            for data in getMusicdata.iter_content():
                fp.write(data)
        time.sleep(random.randint(2, 5))
        send_file_msg(botID, to_wxID, Music_downPath)
    elif mode == 'video':
        Video_downPath = basedir + '\\user_img\\' + sjc + '.mp4'
        getVideodata = requests.get(url=jsonarr['play'], stream=True, headers=headers)
        with open(Video_downPath, 'wb') as fp:
            for data in getVideodata.iter_content():
                fp.write(data)
        time.sleep(random.randint(3, 6))
        send_video_msg(botID, to_wxID, Video_downPath)


# 搜歌听歌功能
def cloud163(text, mode, botID, to_wxID, member_wxID):
    # mode参数，false为搜歌，true为选歌
    if mode:
        songCh = {'m1': 0, 'm2': 1, 'm3': 2, 'm4': 3, 'm5': 4}
        songID = []
        path = basedir + "\\music_doc\\" + member_wxID + '.txt'
        with open(path) as fp:
            for ftext in fp.readlines():
                ftext = ftext.strip()
                songID.append(ftext)
            fp.close()
        time.sleep(random.randint(1, 3))
        send_music_msg(botID, to_wxID, songID[songCh[text]])
        os.remove(path)
    else:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
        }
        html = requests.get(url='https://music.cyrilstudio.top/search?keywords=' + text, headers=headers)
        jsonarr = json.loads(html.content.decode('utf-8'))
        if jsonarr['code'] == 200:
            mu1 = jsonarr['result']['songs'][0]
            mu2 = jsonarr['result']['songs'][1]
            mu3 = jsonarr['result']['songs'][2]
            mu4 = jsonarr['result']['songs'][3]
            mu5 = jsonarr['result']['songs'][4]
            with open(basedir + "\\music_doc\\" + member_wxID + '.txt', "w+") as fp:
                fp.write(
                    str(mu1['id']) + "\n" + \
                    str(mu2['id']) + "\n" + \
                    str(mu3['id']) + "\n" + \
                    str(mu4['id']) + "\n" + \
                    str(mu5['id']) + "\n"
                )
                fp.close()
            Allresult = "======请问听哪首?======\n" + \
                        "m1：" + mu1['name'] + ' - ' + mu1['album']['name'] + "\n" + \
                        "m2：" + mu2['name'] + ' - ' + mu2['album']['name'] + "\n" + \
                        "m3：" + mu3['name'] + ' - ' + mu3['album']['name'] + "\n" + \
                        "m4：" + mu4['name'] + ' - ' + mu4['album']['name'] + "\n" + \
                        "m5：" + mu5['name'] + ' - ' + mu5['album']['name'] + "\n\n" + \
                        "防止刷屏,限制搜索5个结果,直接发送对应序列号即可获取歌曲链接,取消选取歌曲状态发送 听歌# 即可\n" + \
                        "======================="
            time.sleep(random.randint(1, 3))
            send_text_msg(botID, to_wxID, Allresult)
        else:
            send_text_msg(botID, to_wxID, "ERROR：搜不到该歌曲！")
